package class202205.demo20.demo20;

import java.util.Arrays;

public class demo20220520 {

    public static void main(String[] args) {
        int[] arr = {15,85,65,45,25,32};
        System.out.println(avg(arr));
    }

    public static double avg(int[] arr) {
        int sum = 0;
        for (int x : arr) {
            sum += x;
        }
        return (double) sum / (double) arr.length;
    }

    public static void main14(String[] args) {
        int[] array = {1,2,3,4};
        int[] copy = array.clone();

        System.out.println(Arrays.toString(copy));
    }

    public static void main13(String[] args) {
        int[] array = {1,2,3,4};

        int[] copy = new int[array.length];

        //你要拷贝的数组，从0下标开始，拷贝到copy,从0下标开始，拷贝到array.length
        System.arraycopy(array,0,copy,0,array.length);

        System.out.println(Arrays.toString(copy));
    }

    public static void main12(String[] args) {
        int[] array = {1,2,3,4};
        //虽然发生了拷贝，但是可以看作扩容
        int[] copy = Arrays.copyOf(array,2*array.length);
        System.out.println(Arrays.toString(copy));
    }

    public static void main10(String[] args) {
        int[] array = {1,2,3,4};

        int[] copy = new int[array.length];

        for (int i = 0; i < array.length; i++) {
            copy[i] = array[i];
        }

        System.out.println(Arrays.toString(copy));
    }

    public static String myToString(int[] array){
        String str = "[ ";
        for (int i = 0; i < array.length; i++) {
            str +=array[i];
            if (i !=array.length-1){
                str +=",";
            }
        }
        str +=" ]";
        return str;
    }

    public static void main9(String[] args) {
        int[] array = {1,2,3,4};
        String ret = myToString(array);
        System.out.println(ret);
    }

    public static int[] fun(){
        int[] array = {1,2,3,4,5};
        return array;
    }

    public static void main8(String[] args) {
        int[] ret = fun();
        System.out.println(Arrays.toString(ret));
    }

    public static void func2(int[] array) {
        array = new int[]{11,12,13,14};

    }

    public static void func3(int[] array) {
        array[0] = 99;

    }

    public static void main7(String[] args) {
        int[] array1 ={1,2,3,4};
        func2(array1);
        System.out.println(Arrays.toString(array1));

        int[] arr = {100,99,88,77};
        func3(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void func1(int a){
        a = 20;
    }

    public static void main6(String[] args) {
        int x = 10;
        func1(x);
        System.out.println(x);

        int[] array1 = {1,2,3,4,5,6,7,8};
    }

    public static void main5(String[] args) {
        int[] array1 = {1,2,3};
        System.out.println(array1);
        System.out.println("array1:"+Arrays.toString(array1));
        System.out.println("=======");

        array1 = new int[]{99,66,55};
        System.out.println(array1);
        System.out.println("array1:"+Arrays.toString(array1));
        System.out.println("===========");

        array1 = new int[]{65,53,5451,5,6};
        System.out.println(array1);
        System.out.println("array1:"+Arrays.toString(array1));
    }

    public static void main4(String[] args) {
        int[] array1 = {1,2,3,4,5,6};
        int[] array2 = {1,2,3,4,5,6};

        System.out.println(array1);
        System.out.println(array2);
        System.out.println("=========");

        array1 = array2;
        System.out.println(array1);
        System.out.println(array2);
    }


    public static void main3(String[] args) {
        int[] array1 = {1,2,3,4,5,6};
        int[] array2 = array1;

        System.out.println("array1:"+Arrays.toString(array1));
        System.out.println("array2:"+Arrays.toString(array2));

        System.out.println("=============");
        array2[0] = 99;
        System.out.println("array1:"+Arrays.toString(array1));
        System.out.println("array2:"+Arrays.toString(array2));
    }


    public static void main2(String[] args) {
        int a = 0;
        System.out.println(a);

        int[] array = null;
        System.out.println(array.length);
    }

    public static void main1(String[] args) {
        int[] array = {1,2,3};
        System.out.println(array);
        System.out.println(Arrays.toString(array));
        int a = 10;
        System.out.println(a);
    }
}
